package com.ocom.websocket.websocket;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.WebApiWsMessageVo;
import com.ocom.common.vo.recode.DoorRecodeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
public class WSController {

	private final Logger logger = LoggerFactory.getLogger(WSController.class);

	/**
	 * 注入消息处理器
	 */
	@Autowired
	private MyHandler myWsMsgHandler;

	@Autowired
	private WsShare wsShare;


	@GetMapping("/sendMsg111")
	public  String sendMsg23(String id,String msg) {
		wsShare.send("1","2223434");
		return  "succ";
	}


	@PostMapping("/sendMsg")
	public  String sendMsg(String id,String msg) {
		try {

			wsShare.send("1","3444");
			Integer send = myWsMsgHandler.sendToUser(id,msg);
			switch (send){
				case 0:
					return "设备不在线";
				case 1:
					return "发送成功";
				case 2:
					return "发送失败";
				default:
					break;
			}
		}catch (Exception e){
			System.out.println(e);
		}
		return "异常";
	}


	@PostMapping(WebApiServiceUrl.URL_WebSocketMsg)
	public Result webSocketMsg(@RequestBody DoorRecodeVo doorRecodeVo){

		Map<String, Object> objectMap = new HashMap<>();

		objectMap.put("encry", "");
		objectMap.put("data", doorRecodeVo.getData());
		objectMap.put("operateType", doorRecodeVo.getOpenMode());
		objectMap.put("openState", doorRecodeVo.getOpenState());
		objectMap.put("openTime", doorRecodeVo.getOpenTime());
		objectMap.put("sysTime", new Date().getTime());
		objectMap.put("type", 1);

		objectMap.put("idCard", doorRecodeVo.getPCode());
		objectMap.put("pName", doorRecodeVo.getPName());
		objectMap.put("pPhone", doorRecodeVo.getPPhone());
		objectMap.put("codeColor", doorRecodeVo.getCodeColor());
		objectMap.put("colorTime", doorRecodeVo.getColorTime());
		objectMap.put("ymNum", doorRecodeVo.getYmNum());
		objectMap.put("ymTime", doorRecodeVo.getYmTime());
		objectMap.put("hsResult", doorRecodeVo.getHsResult());
		objectMap.put("hsTime", doorRecodeVo.getHsTime());

	//	WsShare share=new WsShare();
		wsShare.send(doorRecodeVo.getComId().toString(),JSON.toJSONString(objectMap));
//		myWsMsgHandler.sendToUser(webSockerMsgVo.getCompanyId(),JSON.toJSONString(objectMap));
		return ResultUtil.success();
	}

	//转发到当前机器的Ws
	@PostMapping("/sendWs")
	public void WsCenterForward(@RequestBody WebApiWsMessageVo webApiWsMessageVo){
		myWsMsgHandler.sendToUser(webApiWsMessageVo.getCompanyId(),webApiWsMessageVo.getMessage());
	}



}


/**
 * 	try {
 * //			byte[] yt6 = Base64.getDecoder().decode(webSockerMsgVo.getYct().getBytes(StandardCharsets.UTF_8));
 * //			String uy = new String(yt6);
 * 			String uy = new String(decoder.decodeBuffer(webSockerMsgVo.getYct()), "UTF-8");
 * 			Map<String, Object> map = JSON.parseObject(uy, Map.class);
 * 			List<Map> list = JSON.parseObject((String) map.get("data"), List.class);
 * 			for (Map<String, Object> map1 : list) {
 * 				for (Map.Entry entry : map1.entrySet()) {
 * 					stringObjectMap.put((String) entry.getKey(), entry.getValue());
 *                                }* 			}
 * 			YtcResult ytcResult= JSON.parseObject(JSON.toJSONString(stringObjectMap),YtcResult.class);
 * 			String idCard ="";
 * 			try {
 * 				idCard = ytcResult.getSqfydsryResult().getIdentityNum();
 * 			}catch (Exception e){
 * 			}
 * 			if(idCard.equals("")){
 * 				try {
 * 					idCard = ytcResult.getIdentityResult().getIdentity();* 				}catch (Exception e){
 * 				}
 * 			}
 * 			objectMap.put("idCard", StringUtils.desValue(idCard,4,4,"*"));
 *
 * 			String usName=ytcResult.getSqfydsryResult().getName();
 * 			if(usName.length()==2){
 * 				usName=usName.substring(0,1)+"*";
 * 			}else {
 * 				usName=StringUtils.desValue(usName,1,1,"*");
 * 			}
 * 			objectMap.put("pName",usName);
 * 			objectMap.put("pPhone","");
 *
 * 			try{
 * 				objectMap.put("pPhone",StringUtils.desValue(ytcResult.getHsResult().getData().getData().get(0).getSJHM(),3,3,"*"));
 * 			}catch (Exception e){
 * 			}
 *
 * 			objectMap.put("codeColor",ytcResult.getYkmResult().getData().getData().get(0).getJKMYS());
 * 			try{
 * 				objectMap.put("colorTime",ytcResult.getSkmResult().getData().getCheckTime());
 * 			}catch (Exception e){
 * 				objectMap.put("colorTime",DateUtil.getDateTime());
 * 			}
 *
 * 			List<hsResultDataDataVo> data34= ytcResult.getHsResult().getData().getData();
 * 			Date hs1=null;  //时间
 * 			String hs2="";	//结果
 * 			try {
 * 				for (hsResultDataDataVo vo:data34){
 * 					Date t= DateUtil.getDate(vo.getJCJGTBSJ(),"yyyy-MM-dd HH:mm:ss");
 * 					if(hs1==null || hs1.compareTo(t)<0){
 * 						hs1=t;
 * 						hs2=vo.getJCJG();
 * 					}
 * 				}
 * 			}catch (Exception e){
 * 			}
 * 			if(hs1!=null){
 * 				objectMap.put("hsResult",hs2);
 * 				objectMap.put("hsTime",DateUtil.getDateTime(hs1));
 * 			}
 * 			List<hs1ResultDataDataVo> data78= ytcResult.getHs1Result().getData().getData();
 * 			try {
 * 				for (hs1ResultDataDataVo vo:data78){
 * 					Date t= DateUtil.getDate(vo.getJc_rq(),"yyyy-MM-dd HH:mm:ss");
 * 					if(hs1==null || hs1.compareTo(t)<0){
 * 						hs1 = t;
 * 						hs2=vo.getJcjg();
 * 					}
 * 				}
 * 			}catch (Exception e){
 * 			}
 * 			if(hs1!=null){
 * 				objectMap.put("hsResult",hs2);
 * 				objectMap.put("hsTime",hs1);
 * 			}else{
 * 				objectMap.put("hsResult","未核酸");
 * 				objectMap.put("hsTime",new Date());
 * 			}
 * 			List<ymResultDataDataVo> data56 = ytcResult.getYmResult().getData().getData();
 * 			Date ymTime=null;  //时间
 * 			try {
 * 				for (ymResultDataDataVo vo:data56){
 * 					Date t= DateUtil.getDate(vo.getInoculationdate(),"yyyy-MM-dd HH:mm:ss");
 * 					if(ymTime==null || ymTime.compareTo(t)<0){
 * 						ymTime=t;
 * 					}
 * 				}
 * 			}catch (Exception e){
 * 			}
 * 			if(ymTime!=null){
 * 				objectMap.put("ymNum",ytcResult.getYmResult().getData().getData().size());
 * 				objectMap.put("ymTime",DateUtil.getDateTime(ymTime));
 * 			}else{
 * 				objectMap.put("ymNum",0);
 * 				objectMap.put("ymTime",DateUtil.getDateTime());
 *                    }
 * 		}catch (Exception e){
 * 			logger.info("【提交大屏展示信息#数据解析异常yct】,{}",e.getMess        ());
 * 		}
 * 		Map<String, Object> map1 = new HashMap<>();
 * 		try {
 * 			map1=JSON.parseObject(webSockerMsgVo.getData(), Map.        ss);
 * 		}catch (Exception e){
 * 			logger.info("【提交大屏展示信息#数据解析异常data】,{}",e.getMess        ());
 * 		}
 */